<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="项目名称" prop="title">
				<el-input v-model="form.title" placeholder="项目名称" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="项目级别" prop="proLevel">
						<sc-select v-model="form.proLevel" :params="{code:'proLevel'}" :apiObj="$API.project.proDic.getProDicByCode" clearable filterable style="width: 100%" @change="getNoticeTemplateListByType"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="项目编号" prop="proCode">
						<el-input v-model="form.proCode" placeholder="项目编号" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="项目类型" prop="proSort">
						<el-cascader v-model="form.proSort" :options="groups" :props="groupsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="项目审批人" prop="approvalUser">
						<el-select v-model="form.approvalUser">
							<el-option :value="item.accountId" :label="item.userName" v-for="item in userList" :key="item.accountId"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="开始时间" prop="beginTime">
						<el-date-picker v-model="form.beginTime" placeholder="开始时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker v-model="form.endTime" placeholder="结束时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="项目经理" prop="manager">
						<select-user-dialog v-model:accountId="form.manager" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="项目预算" prop="budget">
						<el-input-number v-model="form.budget" placeholder="项目预算" clearable ></el-input-number>/元
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="任务负责人" prop="taskUsers">
				<select-user-dialog v-model:accountId="form.taskUsers" :mode="true"/>
			</el-form-item>
			<el-form-item label="参与部门">
				<select-dept-dialog v-model:deptId="form.joinDept" :mode="true"/>
			</el-form-item>
			<el-form-item label="项目内容">
				<el-input v-model="form.remark" placeholder="项目内容" clearable type="textarea" maxlength="380"></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'project'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import AttachMenu from '@/views/module/attachMenu'
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,
		selectDeptDialog,
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			btName: "保存",
			titleMap: {
				add: '项目立项对话框',
				edit: '编辑项目对话框',
				show: '查看'
			},
			userList:[],
			//所需数据选项
			groups: [],
			groupsProps: {
				label:"name",
				value: "sortId",
				emitPath: false,
				checkStrictly: true
			},
			fileList:[],
			apiObj:this.$API.file.attach.uploadFile,
			form: {
				proId:'',
				title: '',
				proLevel: '',
				proCode: '',
				proSort:'',
				approvalUser:'',
				beginTime:'',
				endTime:'',
				manager: '',
				budget: 0,
				taskUsers: '',
				joinDept:'',
				remark:'',
				attachId:'',
			},
			rules: {
				title: [
					{required: true, message: '项目标题不能为空', trigger: 'blur'}
				],
				proCode: [
					{required: true, message: '项目编号不能为空'}
				],
				proSort: [
					{required: true, message: '项目类型不能为空'}
				],
				approvalUser: [
					{required: true, message: '项目审批人不能为空'}
				],
				proLevel: [
					{required: true, message: '项目级别不能为空'}
				]
			}
		}
	},
	mounted() {
		this.getGroup();
		this.getProApprovalUserList();
	},
	methods: {
		async getGroup(){
			var res = await this.$API.project.proSort.getProSortTree.get();
			this.groups = res.data;
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.project.proRecord.insertProRecord.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.project.proRecord.updateProRecord.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.proId = data.proId
			this.form.title = data.title
			this.form.proLevel = data.proLevel
			this.form.proCode = data.proCode
			this.form.proSort = data.proSort
			this.form.approvalUser = data.approvalUser
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.manager = data.manager
			this.form.budget = data.budget
			this.form.taskUsers = data.taskUsers
			this.form.joinDept = data.joinDept
			this.form.remark = data.remark
			this.form.attachId = data.attachId
		},
		async getProApprovalUserList()
		{
			var res = await this.$API.project.proRole.getProApprovalUserList.get();
			if(res.code == 200){
				this.userList = res.data;
			}
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
